package com.jshoperxms.controller.mall.backstage.order;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.vo.order.BackOrderVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.MemberCartEnums;
import com.jshoperxms.controller.utils.enums.ProductEnums;
import com.jshoperxms.controller.utils.maths.Arith;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberCartT;
import com.jshoperxms.entity.MemberDeliverAddressT;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.ProductT;
import com.jshoperxms.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 17/2/7.
 * Des:
 */

@Controller
@RequestMapping("/mall/order")
public class OrderTController extends BaseTController  {


    @Resource
    private MemberTService memberTService;

    @Resource
    private MemberDeliverAddressTService memberDeliverAddressTService;
    @Resource
    private ProductTService productTService;
    @Resource
    private MemberCartTService memberCartTService;
    @Resource
    private OrderTService orderTService;
    //数据形式
    private Map<String, Object> responsedata = new HashMap<>();


    /**
     * 后台保存订单
     * @param rq
     * @return
     */
    @RequestMapping(value="/saveBackOrder",method= RequestMethod.POST)
    public ResponseEntity saveBackOrder(@RequestBody BackOrderVo rq) {
        if (StringUtils.isBlank(rq.getMemberdeliveraddressid()) && StringUtils.isBlank(rq.getMemberinvoiceid())) {
            responsedata.put(TagWords.SUCCESS, false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        } else {
            List<MemberCartT> memberCartTList = new ArrayList<>();
            MemberT memberT = new MemberT();
            //获取会员id
            MemberDeliverAddressT mda = this.memberDeliverAddressTService.findByPK(MemberDeliverAddressT.class, rq.getMemberdeliveraddressid());
            if (mda != null) {
                rq.setMemberid(mda.getMemberid());
                memberT = this.memberTService.findByPK(MemberT.class, rq.getMemberid());
                if (memberT != null) {
                    rq.setMembername(memberT.getLoginname());
                }
            }
            //把选择的商品加入购物车
            if (StringUtils.isNotBlank(rq.getSelectproducts())) {
                String ids[] = StringUtils.split(rq.getSelectproducts(), StaticKey.SPLITDOT);
                for (String s : ids) {
                    ProductT p = this.productTService.findByPK(ProductT.class, s);
                    if (p != null) {
                        MemberCartT bean = new MemberCartT();
                        bean.setId(this.serial.Serialid(SerialKeys.MEMBERCART));
                        bean.setAddtime(DateTools.getSystemTimeBi());
                        bean.setBasicuserid(BaseTools.getAdminCreateId());
                        bean.setBasicusername(BaseTools.getAdminCreateName());
                        bean.setCartactiontag(MemberCartEnums.MemberCartActionTag.SYSGIFT_EIGHT_NUM.getState());
                        bean.setCartstatus(MemberCartEnums.MemberCartStatus.NEWADDTOCART_ONE_NUM.getState());
                        bean.setCarttag(StaticKey.EMPTY);
                        bean.setChangeprice(BigDecimal.valueOf(Arith.sub(p.getSaleprice().doubleValue(), p.getMemberprice().doubleValue())));
                        bean.setGoodsid(p.getGoodsid());
                        bean.setHtmlpath(p.getHtmlpath());
                        if (StringUtils.isBlank(p.getStoreid())) {
                            bean.setIsmall(MemberCartEnums.MemberCartIsMall.SELF_ONE_NUM.getState());
                        } else {
                            bean.setIsmall(MemberCartEnums.MemberCartIsMall.STORE_TWO_NUM.getState());
                        }
                        bean.setIswishlist(MemberCartEnums.MemberCartIsWishList.DISADD_TWO_NUM.getState());
                        bean.setMainimg(p.getMainimg());
                        bean.setMemberid(rq.getMemberid());
                        bean.setMembername(rq.getMembername());
                        bean.setMemberprice(p.getMemberprice());
                        bean.setNeedquantity(1);
                        bean.setPoints(p.getPoints());
                        bean.setProductid(p.getProductid());
                        bean.setProductname(p.getProductname());
                        bean.setPromotioncometag(MemberCartEnums.MemberCartPromotionComeTag.SYSGIFT_EIGHT_NUM.getState());
                        bean.setPsn(p.getPsn());
                        bean.setRporderid(StaticKey.EMPTY);
                        bean.setSaleprice(p.getSaleprice());
                        bean.setSamecartbatchid(StaticKey.EMPTY);
                        bean.setSmainimg(p.getSmainimg());
                        bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                        if (p.getStock() > 0) {
                            bean.setStockstatus(ProductEnums.ProductStock.HAVE.getState());
                        } else {
                            bean.setStockstatus(ProductEnums.ProductStock.NO.getState());
                        }
                        bean.setStoreid(p.getStoreid());
                        bean.setStorename(p.getStorename());
                        bean.setSubtotal(BigDecimal.valueOf(Arith.add(bean.getMemberprice().doubleValue(), Double.valueOf(bean.getNeedquantity()))));
                        bean.setUpdatetime(DateTools.getSystemTimeBi());
                        bean.setVersiont(1);
                        bean.setWeight(p.getWeight());
                        bean.setWeightunit(p.getWeightunit());
                        bean.setPskuvals(p.getPskuvals());
                        bean.setCreatetime(DateTools.getSystemTimeBi());
                        this.memberCartTService.save(bean);
                        memberCartTList.add(bean);
                    }
                }
                this.orderTService.createBackOrderTranscational(memberCartTList, memberT, rq.getMemberdeliveraddressid(), rq.getLogisticsid(), rq.getPaymentid(), rq.getMemberinvoiceid(), StaticKey.EMPTY, rq.getShouldpay(), rq.getFreight());
            }
        }
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

}
